package com.knowledgebooks.nlp;
	import java.awt.BorderLayout;
	import java.awt.Button;
	import java.awt.Checkbox;
	import java.awt.CheckboxGroup;
	import java.awt.Color;
	import java.awt.Dimension;
	import java.awt.Event;
	import java.awt.Font;
	import java.awt.Graphics;
	import java.awt.GridBagConstraints;
	import java.awt.GridBagLayout;
	import java.awt.Insets;
	import java.awt.Label;
	import java.awt.List;
	import java.awt.Panel;
	import java.awt.TextField;
/*==============================================================================
	class PrmPanel (Rajesh Unnikrishnan)

	Purpose:   This is the panel containing all of the parameters, and allows
		   them to be adjusted through control widgets contained within it
	================================================================================*/
	class PrmPanel extends Panel {

		public Button buttonAdd, buttonClr, buttonUpdat, buttonAply, buttonNew;
		public Checkbox checkboxStrt, checkboxHermite, checkboxBspline;   /* choices for line shape */
		public CheckboxGroup cbGrpLines;         /* checkbox group of line types */
		public List listPatterns;		/* choices for pattern specifications */
		public List listRules;
		public TextField textAxiom, textDepth, textScale, textAngle, 
				 textRule, textignore, textClr;

		public int curIdx;	/* index of item in list currently being edited */
		public boolean boolDoApply; 	/* flag whether Apply button has been pressed */
		public boolean buttonGoimgpnl=false;	/* flag whether have been given 
						   associated drawing panel */	
		public WholePanel imagePnl;
	    public Color aColors[];     /* array of colors */	

		public GlobalVariables globlvar;         /* GlobalVariables */
		

		/*----------------------------------------------
		  Constructor:  PrmPanel
	

		  Purpose:      Sets up the control panel containing the
			        user specified parameters for performing
				the LSystems image

		  ---------------------------------------------*/
		public PrmPanel() {
			Insets lft_spc = new Insets(0,5,0,0);

			initColors();

			boolDoApply = false;
			buttonGoimgpnl = false;

			Panel lPanel = new Panel();
			Panel rPanel = new Panel();
			Panel mPanel = new Panel();
			ClrPanel bPanel = new ClrPanel(aColors);

			GridBagLayout gridbag = new GridBagLayout();
			GridBagConstraints c = new GridBagConstraints();
			setLayout(gridbag);
			
			c.fill = GridBagConstraints.HORIZONTAL;	/* fill horizontally*/
			c.weightx=2;  			        /* each of the 3 panels
								   get same horiz space */
			c.insets = new Insets(0,0,0,10); 	/* pad left and right */

			c.gridwidth=2;
			c.anchor = GridBagConstraints.NORTH;

			gridbag.setConstraints(lPanel,c);
			gridbag.setConstraints(rPanel,c);

			gridbag.setConstraints(mPanel,c);
		
			add(lPanel);			/* add panels left to right */
			add(mPanel);	
			add(rPanel);	
			
			/*------------
			  color panel
			  ------------*/

			c = new GridBagConstraints();
			c.gridx = 0;
			c.gridwidth = GridBagConstraints.REMAINDER;
			c.insets = new Insets(15,0,0,0);
			c.fill = GridBagConstraints.HORIZONTAL;
			c.anchor = GridBagConstraints.WEST;
			gridbag.setConstraints(bPanel,c);
			add(bPanel);

			gridbag = new GridBagLayout();
			bPanel.setLayout(gridbag);

			GridBagConstraints constrnts = new GridBagConstraints();

			constrnts.gridwidth = 1;    /* first in row */
			constrnts.fill = GridBagConstraints.HORIZONTAL;
			constrnts.weightx = 1;

			constrnts.insets = new Insets(5,20,5,0);
			Label label8 = new Label("0");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);

			constrnts.insets= new Insets(5,59,5,0);
			label8 = new Label("5");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);

			label8 = new Label("10");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);

			label8 = new Label("15");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);

			label8 = new Label("20");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);

			label8 = new Label("25");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);


			label8 = new Label("30");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);

			label8 = new Label("35");
			gridbag.setConstraints(label8, constrnts);
			bPanel.add(label8);

			constrnts.insets = new Insets(5,10,5,5);
			textClr = new TextField(3);
			gridbag.setConstraints(textClr, constrnts);
			bPanel.add(textClr);	

			gridbag = new GridBagLayout();
			c = new GridBagConstraints();
			setFont(new Font("Helvetica", Font.PLAIN, 14));
			lPanel.setLayout(gridbag);

			c.fill = GridBagConstraints.NONE;  /* stretch components left to right */

			c.fill = GridBagConstraints.HORIZONTAL;  /* stretch components left to right */

			/*----------------------
			  Lindenmayer system Samples
			  ---------------------*/
			GridBagConstraints l_sample = new GridBagConstraints();
			l_sample.gridx = 0;
			l_sample.gridy = 0;
			l_sample.gridwidth = GridBagConstraints.REMAINDER;


			/* Rules Label */
			l_sample.anchor = GridBagConstraints.WEST;
			Label label7 = new Label("Sample Lindenmayer system:");
			gridbag.setConstraints(label7,l_sample);
			lPanel.add(label7);


			GridBagConstraints gbagConstr = new GridBagConstraints();
			gbagConstr.gridwidth = 1;
			gbagConstr.fill = GridBagConstraints.HORIZONTAL;
			gbagConstr.weightx = 1;

			gbagConstr.gridx=0;
			gbagConstr.gridy=1;
			TextField temp = new TextField(25);
			gridbag.setConstraints(temp, gbagConstr);
			temp.hide();
			lPanel.add(temp);
			temp.show();
			temp.hide();

			/* List of Samples LSystem Patterns */
			l_sample.fill = GridBagConstraints.HORIZONTAL;
			l_sample.anchor = GridBagConstraints.WEST;
			l_sample.gridy = 1;
			listPatterns = new List(6,false);
			listPatterns.clear();
			gridbag.setConstraints(listPatterns, l_sample);
			lPanel.add(listPatterns);
			selectd_sampl(listPatterns);



			gbagConstr = new GridBagConstraints();
			gbagConstr.gridwidth = 1;
			gbagConstr.fill = GridBagConstraints.HORIZONTAL;
			gbagConstr.weightx = 1;
			gbagConstr.insets = new Insets (30,0,0,0);


			/* Apply button */
			buttonAply = new Button("Apply");
			gridbag.setConstraints(buttonAply, gbagConstr);
			lPanel.add(buttonAply);
			

			gridbag = new GridBagLayout();
			c = new GridBagConstraints();
			setFont(new Font("Helvetica", Font.PLAIN, 14));
			mPanel.setLayout(gridbag);
			c.fill = GridBagConstraints.NONE;  /* stretch components left to right */

			/*----------------------
			  Rule Controls
			  ---------------------*/
			GridBagConstraints c_rule = new GridBagConstraints();
			c_rule.gridx = 0;
			c_rule.insets=lft_spc;
			c_rule.gridwidth = GridBagConstraints.REMAINDER;

			/* Rules Label */
			c_rule.anchor = GridBagConstraints.WEST;
			Label label5 = new Label("Rules:");
			gridbag.setConstraints(label5,c_rule);
			mPanel.add(label5);


			/* List of Rules */
			c_rule.fill = GridBagConstraints.HORIZONTAL;
			c_rule.anchor = GridBagConstraints.WEST;
			listRules = new List(4, false);
			gridbag.setConstraints(listRules,c_rule);
			mPanel.add(listRules);
			listRules.clear();

			/* rule edit box */
			c_rule.insets = new Insets(35,5,0,0);
			c_rule.anchor = GridBagConstraints.WEST;	
			c_rule.fill = GridBagConstraints.NONE;
			textRule = new TextField(27);
			gridbag.setConstraints(textRule, c_rule);
			
			/*------------------
			   Buttons
		   	 )
			  ------------------*/

			gbagConstr = new GridBagConstraints();
			gbagConstr.insets=lft_spc;
			gbagConstr.gridwidth = 1;
			gbagConstr.fill = GridBagConstraints.HORIZONTAL;
			gbagConstr.weightx = 1;

			/* rule editing buttons */
			
			buttonNew = new Button("Clear");
			
			gbagConstr.gridwidth = GridBagConstraints.REMAINDER;  /* last-in-row */
			gridbag.setConstraints(buttonNew, gbagConstr);

			mPanel.add(buttonNew);


			gridbag = new GridBagLayout();
			c = new GridBagConstraints();
			setFont(new Font("Helvetica", Font.PLAIN, 14));
			rPanel.setLayout(gridbag);

			c.fill = GridBagConstraints.NONE;  /* don't stretch components */
			

			cbGrpLines = new CheckboxGroup();


			/*-------------------
			  Line Checkboxes
			  ------------------*/
			GridBagConstraints c_line = new GridBagConstraints();
			c_line.gridx = 0;		
			c_line.anchor = GridBagConstraints.WEST;
			c_line.gridwidth = GridBagConstraints.REMAINDER; /* last-in-row */



			/* Straight Line Checkbox*/

			c_line.insets= new Insets(20,50,0,0);
			checkboxStrt = new Checkbox("Straight Lines",cbGrpLines,true);
			gridbag.setConstraints(checkboxStrt, c_line);
			rPanel.add(checkboxStrt);


			/* Hermite Curve Checkbox*/
		
			c_line.insets= new Insets(0,50,0,0);
			checkboxHermite = new Checkbox("Hermite Curved Lines",cbGrpLines,false);
			gridbag.setConstraints(checkboxHermite, c_line);
			rPanel.add(checkboxHermite);


			/* BSpline Curve Checkbox*/
			
			/*-------------------
			  Parameter Text Boxes 

			  Axiom, ignore, Depth, Angle, Scale
			  ------------------*/

			GridBagConstraints c_textb = new GridBagConstraints();

			/* Axiom */
			c_textb.gridwidth = 1;    /* first in row */
			c_textb.anchor = GridBagConstraints.WEST;
			Label label1 = new Label("Axiom: ");
			gridbag.setConstraints(label1, c_textb);
			rPanel.add(label1);
			

			c_textb.anchor = GridBagConstraints.WEST;  /* reset */
			c_textb.gridwidth = GridBagConstraints.REMAINDER;/* last-in-row */
			textAxiom = new TextField(26);
			gridbag.setConstraints(textAxiom, c_textb);
			rPanel.add(textAxiom);


			/* ignore */
			c_textb.gridwidth = 1;    /* first in row */
			c_textb.anchor = GridBagConstraints.WEST;
			Label label6 = new Label("ignore: ");
			gridbag.setConstraints(label6, c_textb);
			rPanel.add(label6);
			
			c_textb.anchor = GridBagConstraints.WEST;  /* reset */
			c_textb.gridwidth = GridBagConstraints.REMAINDER;/* last-in-row */
			textignore = new TextField(26);
			gridbag.setConstraints(textignore, c_textb);
			rPanel.add(textignore);


			/* Depth */
			c_textb.gridwidth = 1;         /* first in row */
			c_textb.anchor = GridBagConstraints.WEST;
			Label label2 = new Label("Depth:");
			gridbag.setConstraints(label2, c_textb);
			rPanel.add(label2);


			c_textb.anchor = GridBagConstraints.WEST;  /* reset */
			textDepth = new TextField(1);
			gridbag.setConstraints(textDepth, c_textb);
			rPanel.add(textDepth);


			/* Angle */
			c_textb.anchor = GridBagConstraints.WEST;  /* reset */
	 		Label label3 = new Label("Angle:");
			gridbag.setConstraints(label3, c_textb);
			rPanel.add(label3);


			c_textb.anchor = GridBagConstraints.WEST;  /* reset */
			textAngle = new TextField(3);
			gridbag.setConstraints(textAngle, c_textb);
			rPanel.add(textAngle);


			/* Scale */
			c_textb.anchor = GridBagConstraints.WEST;  /* reset */
			Label label4 = new Label("Scale:");
			gridbag.setConstraints(label4, c_textb);
			rPanel.add(label4);

			
			c_textb.anchor = GridBagConstraints.WEST;  /* reset */
			textScale = new TextField(3);
			gridbag.setConstraints(textScale, c_textb);
			rPanel.add(textScale);


			
			lPanel.show();
			rPanel.show();
			mPanel.show();
			bPanel.show();

			temp.hide();


			listPatterns.select(0);
			changeSpecs(listPatterns.getSelectedIndex());

				
		}
		
		public void setImagePanel(WholePanel pnl_lsys){
			imagePnl = pnl_lsys;
			buttonGoimgpnl = true;
		}


		/*-----------------------------------------------------------
		  Method:  event_handle
		
		  ---------------------------------------------------------*/
		public boolean event_handle(Event e) {
			
			if (e.target instanceof Button) {
				if (e.target==buttonAdd) {
					listRules.addItem(textRule.getText());
					textRule.setText("");
				} else if (e.target==buttonClr) {
					listRules.delItem(curIdx);
					textRule.setText("");
				} else if (e.target==buttonUpdat) {
					listRules.replaceItem(textRule.getText(),curIdx);
					textRule.setText("");
				} else if (e.target==buttonAply) {
					boolDoApply=true;
					/* apply the most recent control panel settings
					   to redraw the image in the related image 
					   panel */
					if (buttonGoimgpnl==true) {
						imagePnl.repaint();
					}

				} else if (e.target==buttonNew) {
					/* clear fields for new LSystems specification */
					listRules.clear();
					textRule.setText("");				
					textAxiom.setText("");
					textDepth.setText("");
					textScale.setText("");
					textAngle.setText("");
					textignore.setText("");
					textClr.setText("");
					boolDoApply=false;
					/* clear the image panel */
					if (buttonGoimgpnl==true) {
						imagePnl.repaint();
					}

					
				}
				return(true);
			} else if (e.target instanceof List) {
				if (e.target==listRules) {
					textRule.setText(listRules.getSelectedItem());
					curIdx = listRules.getSelectedIndex();
					return(true);
				} else if (e.target==listPatterns) {
					changeSpecs(listPatterns.getSelectedIndex());

				}
			}


			return(false);  /* return false for unprocessed events */


		}
		
		public void paint(Graphics g) {

			
		}


		/*======================================================================
		Method:  selectd_sampl
		Purpose:  Define choices for choice menu
		
		======================================================================*/

		public void selectd_sampl(List ChoiceMenu) {
			ChoiceMenu.addItem("Plant in blue");
			ChoiceMenu.addItem("Colored Plant");
		}
	
		/*=====================================================================
		Method:   initColors
		Purpose:  Initialize Array of Colors

		=====================================================================*/
		public void initColors() {

			int i;
			float j;

			int starting;	
			int numShades=5;
			float shadeInc = 1/(float)numShades;
			

			aColors = new Color[globlvar.MAXCOLORS];  /* set array size */
			
			/* White to Black */
			starting = 0;
			for (i=starting, j=1;i<starting+6;j-=shadeInc,i++) {
				aColors[i] = new Color(j,j,j);
			}

			starting = 6;
			/* Red to almost Magenta */
			for (i=starting,j=0;i<starting+5;j+=shadeInc,i++){
				aColors[i] = new Color((float)1,(float)0,j);		
			}
			starting +=5;
			for (i=starting,j=1;i<starting+5;j-=shadeInc,i++){
				aColors[i] = new Color(j,(float)0,(float)1);
			}
			starting +=5;
			for (i=starting,j=0;i<starting+5;j+=shadeInc,i++){
				aColors[i] = new Color((float)0,j,(float)1);		
			}

			
			starting +=5;
			for (i=starting,j=1;i<starting+5;j-=shadeInc,i++){
				aColors[i] = new Color((float)0,(float)1,j);
			}


			
		
			starting +=5;
			for (i=starting,j=0;i<starting+5;j+=shadeInc,i++){
				aColors[i] = new Color(j,(float)1,(float)0);		
			}
				
			/* Yellow to almost Red */
			starting +=5;
			for (i=starting,j=1;i<starting+5;j-=shadeInc,i++){
				aColors[i] = new Color((float)1,j,(float)0);
			}
			
		}


		/*======================================================================
		Method:   changeSpecs

		======================================================================*/

		public void changeSpecs(int Idx) {
					
			textRule.setText("");
			boolDoApply=false;
			/* clear the image panel */
			if (buttonGoimgpnl==true) {
				imagePnl.repaint();
			}


			switch (Idx){
			case 0: 
			textClr.setText("16");
				textAxiom.setText("F");
				textignore.setText("F+-");
				textDepth.setText("5");
				textScale.setText("100");
				textAngle.setText("14");
				listRules.clear();
				listRules.addItem("* <F> * --> F[+F]F[-F]F");
				break;
			case 1: /* Color, Tree E */
				textClr.setText("16");
				textAxiom.setText("X");
				textignore.setText("F+-");
				textDepth.setText("7");
				textScale.setText("100");
				textAngle.setText("18");
				listRules.clear();
				listRules.addItem("* <X> * --> F[+X]F[-X]+X");
				listRules.addItem("* <F> * --> ;;FF::");

				break;


			default:
				textAxiom.setText("");
				textignore.setText("");
				textDepth.setText("");
				textScale.setText("");
				textAngle.setText("");
				listRules.clear();
				break;						

			}

		}


		
	}
